﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Media;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using System.Reflection;

namespace GameSquares
{
   public class GameProcess
    {
       public bool GameStatus { get; set; }
       Random Ran = new Random(Environment.TickCount);
       public GameSetting GameSet = new GameSetting(); //Налаштування
       public GraphItem[] GItems{get;set;}
       public Levels Level = new Levels();
       public int Points { get; set; }
       public int NumberOfBalls { get; set; }
       int _activeItems=1; //для порівняння з numballs

       public void StartNewGame()
       {
           GameStatus = true;
           Level.Number = 0;
           NumberOfBalls = 1;
           GameSet.DRH[5].Score = 0;
           _activeItems = 1;
           ShowGraphItems();
           

       }
       public void ShowGraphItems()
       {         
            List<GraphItem> SGraphItems = new List<GraphItem>();
            ChangeLevel();
           // Список видимих квадратів
           for (int i = 0; i < Global.NumGraphItems; i++)
           {
               GItems[i].Visible = true;
               GItems[i].Active = false;
               GItems[i].Ingame = false;
               GItems[i].BeenClicked = false;               
               SGraphItems.Add(GItems[i]);           
           }
           Level.ShowGraphItems(SGraphItems, NumberOfBalls, Ran);
           NumberOfBalls++;
       }
          
       public bool ChangeLevel()
       {
           if (NumberOfBalls >= 9)
           {
               
               if (Level.Number == 3)
               {
                   Points = 0;
                   GameStatus = false;
                   return false;
               }
               Level.Number++;
               NumberOfBalls = 1;
               return true;
           }
           else return false;
       }

       public void MouseClick(MouseEventArgs e)
       {
           
               for (int j = 0; j < Global.NumGraphItems; j++)
               {
                   GItems[j].Visible = true;
                   if (GItems[j].BeenClicked == false)
                   {
                       GItems[j].Active = false;
                   }
               }
               for (int i = 0; i < Global.NumGraphItems; i++)
               {
                   if (GItems[i].CellCoordinate.Contains(e.X, e.Y) == true)
                   {                     
                       if (GItems[i].Ingame == true)
                       {
                           GItems[i].Active = true;
                           GItems[i].BeenClicked = true;
                           _activeItems++;
                           Points++;
                           GameSet.DRH[5].Score = GameSet.DRH[5].Score + Points;
                           if (_activeItems == (NumberOfBalls))
                           {
                               _activeItems = 1;
                               ShowGraphItems();
                           }
                           else
                           {
                               return;
                           }
                       }
                       else
                       {
                           _activeItems++;
                           Points = 0;
                           if (_activeItems == (NumberOfBalls))
                           {
                               _activeItems = 1;
                               ShowGraphItems();
                           }
                       }
                   }
               
           }
       }

       public void ToFileIni()
       {
           string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\settings.lin";
           using (FileStream fileStream = File.Create(filePath))
           {
               BinaryFormatter bf = new BinaryFormatter();
               for (int i = 0; i < GameSet.DRH.Length; i++)
               {
                   GameSet.DRH[i].CurrentPlayer = false;
               }
               bf.Serialize(fileStream, GameSet);
               fileStream.Close();
           }
       }

       public void FromFileIni( )
       { 
            string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\settings.lin";
            FileInfo fi = new FileInfo(filePath);
            if (fi.Exists == false)
            {
                return;
            }
            using (FileStream fileStream = File.OpenRead(filePath))
            {
                BinaryFormatter bf = new BinaryFormatter();
                GameSet = (GameSetting)bf.Deserialize(fileStream);
                Array.Sort(GameSet.DRH, new SortPlayers());
                fileStream.Close();
            }
            
       }

       void ParseDataPlayers()
       {
           // Порівнюємо індекс до і після ходу
           int index = -1;
           for (int i = 0; i < GameSet.DRH.Length; i++)
           {
               if (GameSet.DRH[i].CurrentPlayer == true)
               {
                   index = i;
                   Array.Sort(GameSet.DRH, new SortPlayers());
                   break;
               }
           }
       }

       // Для сортування рекордів
       public class SortPlayers : IComparer
       {
           // реалізація IComparer
           public int Compare(object o1, object o2)
           {
               DataRecordsman drh1 = (DataRecordsman)o1;
               DataRecordsman drh2 = (DataRecordsman)o2;

               //порівнюємо очки
               int result = drh2.Score.CompareTo(drh1.Score);
               // Перевага тому, хто раніше поставив рекорд
               if (result == 0)
                   result = drh1.CurrentPlayer.CompareTo(drh2.CurrentPlayer);

               return result;
           }
       }
    }
}
